<?

$_list = L_LANGS;
$_table = 'list';				// where info is stored
$_crit = "id_list=$_list";	// data selection criterium
$_order = 'id_index'; // ordered by this field

/*
$intro_page = datafetch('db_item', tb('struct'), 'parent=0');
$main_page = datafetch('db_item', tb('struct'), dbAndL('tag', PT_MAIN));

debug(array($intro_page, $main_page));

$DATA['embed'] = !($intro_page['tag'] & PT_MAIN);
$DATA['skip'] = ($intro_page['kind'] & PK_REDIRECT);
*/


if ($d = (int)param('delete')) {
	
	dataset('db_delete', tb($_table), "id=$d AND $_crit AND !(".dbAnd('attr', LL_CORE).')');

//	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
	exit;
}

if ($a = (int)param('enable')) {
	
	dataset('db_update', tb($_table), "attr = ".dbOr('attr', LL_ACTIVE), "$_crit AND (id=$a)");
	exit;
}

if ($a = (int)param('disable')) {
	
	dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~LL_ACTIVE), "$_crit AND (id=$a)");
	exit;
}

if ($a = (int)param('langdef')) {
	
	dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~LL_DEFAULT), $_crit);
	dataset('db_update', tb($_table), "attr = ".dbOr('attr', LL_DEFAULT), "$_crit AND (id=$a)");
	exit;
}

if ($a = (int)param('admdef')) {
dbdebug();	
	dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~LL_ADMIN), $_crit);
	dataset('db_update', tb($_table), "attr = ".dbOr('attr', LL_ADMIN), "$_crit AND (id=$a)");
	exit;
}


if ($a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'manual':	// ajax request, no data should be returned

			$t = 1;
			
			parse_str(postdata('order'), $temp);

			if (is_array($temp = $temp['objects']) && sizeof($temp)) {

				$DATA['objects'] = datafetch_array('db_list', array(
					'table' => tb($_table), 'where' => $_crit, 'index' => 'id', 'order' => $_order
				));

				foreach($temp as $id) {
					if ($id = (int)$id) {
						if ($DATA['objects'][$id]) {
							dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
							unset($DATA['objects'][$id]);
						}
					}
				}
				
				if (sizeof($DATA['objects']))
					foreach($DATA['objects'] as $id => $v) 
						dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
			}
			
			exit;	// ajax: no more processing needed
			break;
	}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
	exit;
}





switch (action(array('create_object'))) {


	case 'create_object':

		if ($title = trim(postdata('title'))) {

			$max = ($m = datafetch('db_item', tb($_table), $_crit, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
				
			if ($id = datafetch('db_insert', tb($_table), array('id_list' => $_list, 'text' => $title, 'id_index' => $max))) {
				header("Location: edit-$id/"); // redirect to the edit page
				exit;
			}
				// !!! else error on insert - handle it
		}
		
		break;

}
	

$DATA['objects'] = datafetch_array('db_list', array('table' => tb($_table), 'where' => $_crit, 'index' => 'id', 'order' => $_order));

